package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.DispatchOrderLogPageDTO;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.entity.DispatchOrderLog;
import com.ctshk.rpc.system.mapper.DispatchOrderLogMapper;
import com.ctshk.rpc.system.req.DispatchOrderLogPageReq;
import com.ctshk.rpc.system.service.IDispatchOrderLogService;
import com.ctshk.rpc.system.service.ISysUserService;
import com.github.houbb.heaven.util.util.CollectionUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-05-24
 */
@DubboService
public class DispatchOrderLogServiceImpl extends ServiceImpl<DispatchOrderLogMapper, DispatchOrderLog> implements IDispatchOrderLogService {
    @DubboReference
    ISysUserService sysUserService;

    @Override
    public Result page(DispatchOrderLogPageReq pageReq, Long userId) {
        Page<DispatchOrderLog> page = new Page<>(pageReq.getPageNo(), pageReq.getPageSize());
        IPage<DispatchOrderLog> iPage = baseMapper.selectPage(page, new QueryWrapper<DispatchOrderLog>().lambda()
                .eq(DispatchOrderLog::getDispatchId, pageReq.getDispatchId())
                .eq(DispatchOrderLog::getIsDeleted, IsDeletedCode.NO.getCode())
                .orderByDesc(DispatchOrderLog::getGmtCreate));
        List<DispatchOrderLog> records = iPage.getRecords();
        if (CollectionUtil.isEmpty(records)) {
            return Result.success(new PageResponse<DispatchOrderLogPageDTO>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize()));
        }
        List<DispatchOrderLogPageDTO> list = EntityUtil.copyList(records, DispatchOrderLogPageDTO.class);
        Set<Long> userIds = records.stream().map(DispatchOrderLog::getDispatchUserId).collect(Collectors.toSet());
        List<UserDTO> userDTOS = sysUserService.queryCacheUsers(userIds);
        list.forEach(record -> {
            for (UserDTO userDTO : userDTOS) {
                if (userDTO != null && Objects.equals(userDTO.getId(), record.getDispatchUserId())) {
                    record.setDispatchUser(userDTO.getCreateDept());
                    break;
                }
            }
        });
        return Result.success(new PageResponse<DispatchOrderLogPageDTO>(list, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize()));
    }
}
